package com.dsp.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextImpl;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.core.util.BaseUser;
import com.core.util.DspUtil;
import com.core.util.Pagination;
import com.dsp.bean.Jms;
import com.dsp.services.JmsService;

@Controller("JmsController")
public class JmsController extends DspController<Jms>{
	

	public JmsController() {
		super(Jms.class);
	}

	Logger logger = Logger.getLogger(DspUtil.Log4J);

	@Autowired
	private JmsService jmsService;

	@RequestMapping(value = "/jms", method = RequestMethod.GET)
	public ModelAndView list() {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("jms/list");
		mav.addObject("path", "jms/list.jsp");
		mav.addObject("current", "jms");
		return mav;
	}

	@RequestMapping(value = "/jms_list")
	@ResponseBody
	public Pagination<Jms> list(HttpServletRequest request, HttpServletResponse response) {
		super.initHttp(request, response);
		DetachedCriteria dc = DetachedCriteria.forClass(Jms.class);
		SecurityContextImpl upat = (SecurityContextImpl) request.getSession().getAttribute("SPRING_SECURITY_CONTEXT");
		BaseUser bu = (BaseUser) upat.getAuthentication().getPrincipal();
		dc.add(Restrictions.eq("userId", bu.getId()));
		dc.addOrder(Order.desc("jobsTime"));
		Pagination<Jms> p = this.jmsService.search(dc, (page - 1) * rp, rp);
		p.setPage(page);
		return p;
	}

	@RequestMapping(value = "/jms_count")
	@ResponseBody
	public Long count(HttpServletRequest request, HttpServletResponse response) {
		DetachedCriteria dc = DetachedCriteria.forClass(Jms.class);
		SecurityContextImpl upat = (SecurityContextImpl) request.getSession().getAttribute("SPRING_SECURITY_CONTEXT");
		BaseUser bu = (BaseUser) upat.getAuthentication().getPrincipal();
		Long userId = bu.getId();
		dc.add(Restrictions.eq("userId", userId));
		dc.add(Restrictions.eq("status", 0L));
		Pagination<Jms> p = this.jmsService.search(dc, 1, 10000);
		return p.getTotal();
	}
}
